package _go

/**
 * Definition for a Node.
 * type Node struct {
 *     Val int
 *     Left *Node
 *     Right *Node
 *     Next *Node
 * }
 */

func connect(root *Node) *Node {
	if root == nil {
		return root
	}
	connectnode(root.Left, root.Right)
	return root
}

//链接两个节点
func connectnode(root1 *Node, root2 *Node) {
	if root1 == nil || root2 == nil {
		return
	}

	root1.Next = root2

	connectnode(root1.Left, root1.Right)
	connectnode(root2.Left, root2.Right)

	//跨父节点里链接
	connectnode(root1.Right, root2.Left)

}
